package xyz.bali16.module.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.entity.Role;
import xyz.bali16.module.article.entity.Category;
import xyz.bali16.module.article.entity.Tag;
import xyz.bali16.module.article.model.category.CategoryAddParam;
import xyz.bali16.module.article.model.category.CategoryPageParam;
import xyz.bali16.module.article.model.category.CategoryUpdateParam;
import xyz.bali16.module.article.model.tag.TagPageParam;
import xyz.bali16.module.article.service.CategoryService;


import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.article.utils.ResultUtils.errorLog;

/**
 * @article-author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"category")
@Api(tags = "目录接口")
@AllArgsConstructor
public class CategoryController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    CategoryService categoryService;

    @GetMapping
    @ApiOperation("分页条件查询目录")
    //@PreAuthorize("@article-auth.permission('plugin:article:category:query')")
    public Result<IPage<Category>> pageQuery(@Valid CategoryPageParam param) {
        IPage<Category> tagIPage = categoryService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, tagIPage);
    }


    @PostMapping
    //@PreAuthorize("@article-auth.permission('plugin:article:category:add')")
    @ApiOperation("添加目录")
    public Result<String> add(@RequestBody @Valid CategoryAddParam param) {
        try {
            categoryService.addCategory(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增目录 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增目录失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@article-auth.permission('plugin:article:category:update')")
    @ApiOperation("修改目录")
    public Result<String> update(@RequestBody @Valid CategoryUpdateParam param) {
        try {
            categoryService.updateCategory(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新目录 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新目录失败", e);
        }
    }


    @DeleteMapping("/{categoryId}")
    //@PreAuthorize("@article-auth.permission('plugin:article:category:delete')")
    @ApiOperation("删除目录")
    @ApiImplicitParam(name = "categoryId", value = "目录id", paramType = "path", required = true)
    public Result<String> deleteCategory(@PathVariable("categoryId") String categoryId) {
        try {
            categoryService.delete(categoryId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除目录 '{}' 失败. {}", categoryId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
